package com.channel.temperature.collect.app.ble;

/**
 * Created by Administrator on 2017/4/14 0014.
 */


import android.util.Log;

import com.channel.temperature.collect.app.util.MyUtil;


/**
 * @author : YJP
 * @description : 蓝牙锁具/机柜蓝牙命令库 泰然协议
 * @company : Chengdu Tazan.
 * @Date 2020/3/2 8:49
 */

public class BLECommand {


    //初始密钥
    public static byte[] key = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};


    public static byte[] reset() {
        var sk = "LBuiRt9o";
        byte[] cmdreset = {0x03, 0x10, (byte) 0xff};
        SHLockCmd shcmd = new SHLockCmd();
        final byte[] buf = shcmd.sendLockCmd(cmdreset);
        byte[] sbuf = new byte[buf.length - 4];

        Log.e("BLECommand", MyUtil.bytes2Hex(sbuf));
        //将数据部分加密
        System.arraycopy(buf, 3, sbuf, 0, buf.length - 4);
        byte[] rc4sbuf = new byte[sbuf.length];

        Log.e("BLECommand", MyUtil.bytes2Hex(rc4sbuf));

        rc4sbuf = RC4.RC4Calculate(sbuf, sk.getBytes());
        for (int i = 0; i < rc4sbuf.length; i++) {
            buf[3 + i] = rc4sbuf[i];
        }
        return buf;
    }

    public static byte[] setSleepTimeCmd(int minute) {
        byte[] cmd = {0x04, 0x11, 0x00, (byte) 0xff};
        cmd[2] = (byte) minute;
        SHLockCmd shcmd = new SHLockCmd();
        final byte[] buf = shcmd.sendLockCmd(cmd);

        Log.e("测试", "发送加密前的原始数据 1111111" + MyUtil.bytes2Hex(buf));

        byte[] sbuf = new byte[buf.length - 4];

        Log.e("BLECommand", MyUtil.bytes2Hex(sbuf));
        //将数据部分加密
        System.arraycopy(buf, 3, sbuf, 0, buf.length - 4);

        Log.e("测试", "发送加密前的原始数据 " + MyUtil.bytes2Hex(buf));
        byte[] rc4sbuf = new byte[sbuf.length];

        rc4sbuf = RC4.RC4Calculate(sbuf, key);

        for (int i = 0; i < rc4sbuf.length; i++) {
            buf[3 + i] = rc4sbuf[i];
        }
        Log.e("BLECommand", "RC4加密后" + "密钥：" + MyUtil.bytes2Hex(key) + "密文为:" + MyUtil.bytes2Hex(buf));

        return buf;
    }

    public static byte[] setSerialPortCmd(int value) {
        byte[] cmd = {0x04, 0x17, 0x00, (byte) 0xff};
        cmd[2] = (byte) value;
        SHLockCmd shcmd = new SHLockCmd();
        final byte[] buf = shcmd.sendLockCmd(cmd);

        Log.e("测试", "发送加密前的原始数据 1111111" + MyUtil.bytes2Hex(buf));

        byte[] sbuf = new byte[buf.length - 4];

        Log.e("BLECommand", MyUtil.bytes2Hex(sbuf));
        //将数据部分加密
        System.arraycopy(buf, 3, sbuf, 0, buf.length - 4);

        Log.e("测试", "发送加密前的原始数据 " + MyUtil.bytes2Hex(buf));
        byte[] rc4sbuf = new byte[sbuf.length];

        rc4sbuf = RC4.RC4Calculate(sbuf, key);

        for (int i = 0; i < rc4sbuf.length; i++) {
            buf[3 + i] = rc4sbuf[i];
        }
        Log.e("BLECommand", "RC4加密后" + "密钥：" + MyUtil.bytes2Hex(key) + "密文为:" + MyUtil.bytes2Hex(buf));

        return buf;
    }

    public static byte[] setQueryDeviceInfoCMD() {
        byte[] cmd = {0x03, 0x15, (byte) 0xff};
        SHLockCmd shcmd = new SHLockCmd();
        final byte[] buf = shcmd.sendLockCmd(cmd);

        byte[] sbuf = new byte[buf.length - 4];

        //将数据部分加密
        System.arraycopy(buf, 3, sbuf, 0, buf.length - 4);

        byte[] rc4sbuf = new byte[sbuf.length];

        rc4sbuf = RC4.RC4Calculate(sbuf, key);

        for (int i = 0; i < rc4sbuf.length; i++) {
            buf[3 + i] = rc4sbuf[i];
        }

        return buf;
    }

    public static byte[] setCheckCmd(int value1, int value2) {
        byte[] cmd = {0x05, 0x16, 0x00, 0x00, (byte) 0xff};

//        byte[] result = MyUtil.intToBytes(value);
//        String hexResult = MyUtil.bytes2Hex(result);
//        Log.e("setCheckCmd", "hexResult  : " + hexResult);
        cmd[2] = (byte) value1;
        cmd[3] = (byte) value2;
        Log.e("setCheckCmd", "setCheckCmd  : " + MyUtil.bytes2Hex(cmd));
        SHLockCmd shcmd = new SHLockCmd();
        final byte[] buf = shcmd.sendLockCmd(cmd);

        Log.e("测试", "发送加密前的原始数据 1111111" + MyUtil.bytes2Hex(buf));

        byte[] sbuf = new byte[buf.length - 4];

        Log.e("BLECommand", MyUtil.bytes2Hex(sbuf));
        //将数据部分加密
        System.arraycopy(buf, 3, sbuf, 0, buf.length - 4);

        Log.e("测试", "发送加密前的原始数据 " + MyUtil.bytes2Hex(buf));
        byte[] rc4sbuf = new byte[sbuf.length];

        rc4sbuf = RC4.RC4Calculate(sbuf, key);

        for (int i = 0; i < rc4sbuf.length; i++) {
            buf[3 + i] = rc4sbuf[i];
        }
        Log.e("BLECommand", "RC4加密后" + "密钥：" + MyUtil.bytes2Hex(key) + "密文为:" + MyUtil.bytes2Hex(buf));

        return buf;
    }

    public static byte[] setBaudCmd(int baud) {
        byte[] cmd = {0x04, 0x12, 0x00, (byte) 0xff};
        cmd[2] = (byte) baud;
        SHLockCmd shcmd = new SHLockCmd();
        final byte[] buf = shcmd.sendLockCmd(cmd);

        byte[] sbuf = new byte[buf.length - 4];

        //将数据部分加密
        System.arraycopy(buf, 3, sbuf, 0, buf.length - 4);

        byte[] rc4sbuf = new byte[sbuf.length];

        rc4sbuf = RC4.RC4Calculate(sbuf, key);

        for (int i = 0; i < rc4sbuf.length; i++) {
            buf[3 + i] = rc4sbuf[i];
        }

        return buf;
    }

    public static byte[] setVOUTCmd(boolean enOn, boolean on9700) {
        byte[] cmd = {0x05, 0x13, 0x00, 0x00, (byte) 0xff};
        cmd[2] = enOn ? (byte) 0x01 : (byte) 0x00;
        cmd[3] = on9700 ? (byte) 0x01 : (byte) 0x00;
        SHLockCmd shcmd = new SHLockCmd();
        final byte[] buf = shcmd.sendLockCmd(cmd);

        byte[] sbuf = new byte[buf.length - 4];

        //将数据部分加密
        System.arraycopy(buf, 3, sbuf, 0, buf.length - 4);

        byte[] rc4sbuf = new byte[sbuf.length];

        rc4sbuf = RC4.RC4Calculate(sbuf, key);

        for (int i = 0; i < rc4sbuf.length; i++) {
            buf[3 + i] = rc4sbuf[i];
        }

        return buf;
    }

    public static byte[] setQueryElectCmd() {
        byte[] cmd = {0x03, 0x14, (byte) 0xff};
        SHLockCmd shcmd = new SHLockCmd();
        final byte[] buf = shcmd.sendLockCmd(cmd);

        byte[] sbuf = new byte[buf.length - 4];

        //将数据部分加密
        System.arraycopy(buf, 3, sbuf, 0, buf.length - 4);

        byte[] rc4sbuf = new byte[sbuf.length];

        rc4sbuf = RC4.RC4Calculate(sbuf, key);

        for (int i = 0; i < rc4sbuf.length; i++) {
            buf[3 + i] = rc4sbuf[i];
        }

        return buf;
    }

    public static byte[] decodeRC4Bytes(byte[] encodedByte) {
        byte[] back = new byte[encodedByte.length];
        System.arraycopy(encodedByte, 0, back, 0, encodedByte.length);                       //j将原始数据备份一下
        //取出RC4加密的部分
        byte[] raw_data = new byte[back.length - 4];
        System.arraycopy(back, 3, raw_data, 0, back.length - 4);
        //将这部分RC4解密
        byte[] dec_data = RC4.RC4Calculate(raw_data, key);
        //将解密后的数据协会到data中
        for (int i = 0; i < dec_data.length; i++) {
            back[3 + i] = dec_data[i];
        }
        byte[] data = SHLockCmd.Decryption(back, back[back.length - 2], 3, back.length - 5);

        return data;
    }


}
